package org.jlang.components;

import java.util.HashMap;
import java.util.Map;

import org.jlang.declarations.Declaration;
import org.jlang.exceptions.DuplicateDeclarationException;
import org.jlang.interfaces.ParseTreeNode;

/**
 * Simple implementation.
 * Disallows multiple variable declarations
 *
 *
 * @author tomek
 *
 */


public class SimpleSymbolTable extends SymbolTable {

	private Map<String, Declaration> symbolTable;
	private Map<String, Integer> variableNumbers;
	
	public SimpleSymbolTable() {
		symbolTable = new HashMap<String, Declaration>();
		variableNumbers = new HashMap<String, Integer>();
	}
	
	public void add(Declaration declaration) throws DuplicateDeclarationException {
		ParseTreeNode ptn = (ParseTreeNode) declaration;
		
		Declaration old = symbolTable.get(declaration.getName());
		if (old != null) {
			throw new DuplicateDeclarationException(declaration);
		}
		
		symbolTable.put(declaration.getName(), declaration);
		variableNumbers.put(declaration.getName(), variableNumbers.size());
	}
	
	public Declaration getDeclaration(String name) {
		return symbolTable.get(name);
	}
	
	public Integer getVariableNumber(String name) {
		return variableNumbers.get(name);
	}
}
